<!doctype html>
<html lang="en">
	<head>
		<title>three.js webgl - geometry - large mesh</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				background:#000;
				color:#fff;
				padding:0;
				margin:0;
				overflow:hidden;
				font-family:georgia;
				text-align:center;
			}
			h1 { }
			a { color:skyblue }
			canvas { pointer-events:none; z-index:10; }
			#log { position:absolute; top:50px; text-align:left; display:block; z-index:100 }
			#d { text-align:center; margin:1em 0 -18em 0; z-index:0; position:relative; display:block }
			.button { background:orange; color:#fff; padding:0.2em 0.5em; cursor:pointer }
			.inactive { background:#999; color:#eee }
		</style>
	</head>

	<body>
		<div id="d">
			<h1>Large mesh test</h1>

			<span id="rcanvas" class="button inactive">2d canvas renderer</span>
			<span id="rwebgl" class="button">WebGL renderer</span>
			<br/>

			<p>Lucy model from <a href="http://graphics.stanford.edu/data/3Dscanrep/">Stanford 3d scanning repository</a>
			(decimated with <a href="http://meshlab.sourceforge.net/">Meshlab</a>).

			<p>Please be patient while the mesh is loading. It may take a while, it's 2 MB file.

			<br/>
			<p>Best viewed in Chrome or Firefox using WebGL renderer.
			<p>Canvas renderer is very slow for this demo and only diffuse material works there.
			<p>Use the flag --allow-file-access-from-files if working localy in Chrome.
		</div>

		<pre id="log"></pre>

		<script src="../build/Three.js"></script>
		<script src="js/Stats.js"></script>

		<script>

			var SCREEN_WIDTH = window.innerWidth;
			var SCREEN_HEIGHT = window.innerHeight;
			var FLOOR = -250;

			var container, stats;

			var camera, scene, canvasRenderer, webglRenderer;

			var loader;

			var mesh, zmesh, lightMesh;

			var directionalLight, pointLight;

			var mouseX = 0, mouseY = 0;

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;

			var render_canvas = 1, render_gl = 1;
			var has_gl = 0;

			var bcanvas = document.getElementById( "rcanvas" );
			var bwebgl = document.getElementById( "rwebgl" );

			document.addEventListener( 'mousemove', onDocumentMouseMove, false );

			init();
			animate();

			render_canvas = !has_gl;
			bwebgl.style.display = has_gl ? "inline" : "none";
			bcanvas.className = render_canvas ? "button" : "button inactive";

			function addMesh( geometry, scale, x, y, z, rx, ry, rz, material ) {

				mesh = new THREE.Mesh( geometry, material );

				mesh.scale.set( scale, scale, scale );
				mesh.position.set( x, y, z );
				mesh.rotation.set( rx, ry, rz );

				scene.add( mesh );

			}

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				scene = new THREE.Scene();

				camera = new THREE.PerspectiveCamera( 50, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 100000 );
				camera.position.z = 1500;
				scene.add( camera );

				// LIGHTS

				var ambient = new THREE.AmbientLight( 0x101010 );
				scene.add( ambient );

				directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
				directionalLight.position.set( 1, 1, 2 ).normalize();
				scene.add( directionalLight );

				pointLight = new THREE.PointLight( 0xffaa00 );
				pointLight.position.set( 0, 0, 0 );
				scene.add( pointLight );

				// light representation

				sphere = new THREE.SphereGeometry( 100, 16, 8, 1 );
				lightMesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) );
				lightMesh.scale.set( 0.05, 0.05, 0.05 );
				lightMesh.position = pointLight.position;
				scene.add( lightMesh );


				if ( render_gl ) {
					try {

						webglRenderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
						webglRenderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
						webglRenderer.domElement.style.position = "relative";
						container.appendChild( webglRenderer.domElement );
						has_gl = 1;

					}
					catch (e) {
					}
				}

				if( render_canvas ) {

					canvasRenderer = new THREE.CanvasRenderer();
					canvasRenderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
					container.appendChild( canvasRenderer.domElement );

				}


				stats = new Stats();
				stats.domElement.style.position = 'absolute';
				stats.domElement.style.top = '0px';
				stats.domElement.style.zIndex = 100;
				container.appendChild( stats.domElement );

				bcanvas.addEventListener( "click", toggleCanvas, false );
				bwebgl.addEventListener( "click", toggleWebGL, false );

				loader = new THREE.BinaryLoader( true );
				//loader = new THREE.JSONLoader( true );
				document.body.appendChild( loader.statusDomElement );

				var s = Date.now(),
					callback = function( geometry ) { createScene( geometry, s ) };

				//loader.load( 'obj/lucy/Lucy100k_slim.js', callback );
				loader.load( 'obj/lucy/Lucy100k_bin.js', callback );

			}


			function createScene( geometry, start ) {

				addMesh( geometry, 0.75, 900, 0, 0,  0,0,0, new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0x030303, specular: 0x990000, shininess: 30 } ) );
				addMesh( geometry, 0.75, 300, 0, 0,  0,0,0, new THREE.MeshFaceMaterial( ) );
				addMesh( geometry, 0.75, -300, 0, 0, 0,0,0, new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0x111111, specular: 0xffaa00, shininess: 10 } ) );
				addMesh( geometry, 0.75, -900, 0, 0, 0,0,0, new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0x555555, specular: 0x666666, shininess: 10 } ) );

				loader.statusDomElement.style.display = "none";

				log( "geometry.vertices: " + geometry.vertices.length );
				log( "geometry.faces: " + geometry.faces.length );

				log( "model loaded and created in " + ( Date.now() - start ) + " ms" );

			}

			function onDocumentMouseMove( event ) {

				mouseX = ( event.clientX - windowHalfX );
				mouseY = ( event.clientY - windowHalfY );

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

			var r = 0;

			function render() {

				camera.position.x += ( mouseX - camera.position.x ) * .05;
				camera.position.y += ( - mouseY - camera.position.y ) * .05;

				camera.lookAt( scene.position );

				lightMesh.position.x = 700 * Math.cos( r );
				lightMesh.position.z = 700 * Math.sin( r );

				r += 0.01;

				if ( render_canvas ) canvasRenderer.render( scene, camera );
				if ( render_gl && has_gl ) webglRenderer.render( scene, camera );

			}

			function log( text ) {

				var e = document.getElementById( "log" );
				e.innerHTML = text + "<br/>" + e.innerHTML;

			}

			function toggleCanvas() {

				render_canvas = !render_canvas;
				bcanvas.className = render_canvas ? "button" : "button inactive";

				render_gl = !render_canvas;
				bwebgl.className = render_gl ? "button" : "button inactive";

				if( has_gl )
					webglRenderer.domElement.style.display = render_gl ? "block" : "none";

				canvasRenderer.domElement.style.display = render_canvas ? "block" : "none";

			}

			function toggleWebGL() {

				render_gl = !render_gl;
				bwebgl.className = render_gl ? "button" : "button inactive";

				render_canvas = !render_gl;
				bcanvas.className = render_canvas ? "button" : "button inactive";

				if( has_gl )
					webglRenderer.domElement.style.display = render_gl ? "block" : "none";

				canvasRenderer.domElement.style.display = render_canvas ? "block" : "none";

			}

		</script>

	</body>
</html>
